package com.jr.controller;

import com.jr.entry.BlogText;
import com.jr.pojo.PageHelper;
import com.jr.service.BlogTextService;
import com.jr.util.ResultObject;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.concurrent.Callable;


@SuppressWarnings("all")
@RestController /*替换@RestController=@Controller + @ResponseBody*/
@RequestMapping("/Blog/")
public class BlogTextController {
    @Autowired

    private BlogTextService blogTextMapperService;

    @Autowired
    private ResultObject resultObject;

    @RequestMapping("findBlogTextByClickNumber")
    public ResultObject findBlogTextByClickNumber() {
        resultObject.setStatus(400);
        resultObject.setResult(blogTextMapperService.showBlogTextByClickNumber());
        return resultObject;
    }

    /*根据博客id来获得详情页面*/
    @RequestMapping("showBlogTextDetail")
    public ResultObject showBlogTextDetail(BlogText blogText) {
        resultObject.setResult(blogTextMapperService.showBlogTextDetail(blogText));
        return resultObject;
    }

    @RequestMapping("showAll")
    public ArrayList<BlogText> showALl(PageHelper pageHelper) {
        resultObject.setStatus(400);
        RowBounds rowBounds = new RowBounds((pageHelper.getPage() - 1) * 5, 5);
        ArrayList<BlogText> list = blogTextMapperService.showAllBlogText(rowBounds);
        return list;
    }

    @RequestMapping("showJava")
    public ResultObject showByType(BlogText blogText, PageHelper pageHelper) {
        resultObject.setStatus(400);
        RowBounds rowBounds = new RowBounds((pageHelper.getPage() - 1) * 5, 5);
        resultObject.setResult(blogTextMapperService.showBlogTextByType(blogText, rowBounds));
        return resultObject;
    }

    @RequestMapping("showText")
    public ResultObject showTextInBack(BlogText blogText, PageHelper pageHelper) {
        resultObject.setStatus(400);
        RowBounds rowBounds = new RowBounds((pageHelper.getPage() - 1) * 5, 5);
        resultObject.setResult(blogTextMapperService.showBackground(blogText, rowBounds));
        return resultObject;
    }

    /*详情推荐榜前三*/
    @RequestMapping("showSuggest")
    public ResultObject showSuggest(BlogText blogText) {
        resultObject.setStatus(400);
        resultObject.setResult(blogTextMapperService.showSuggest(blogText));
        return resultObject;
    }

    /*添加博客信息*/
    @RequestMapping("insertBlog")
    public ResultObject insertBlog(BlogText blogText) {
        resultObject.setStatus(400);
        if (blogText.getDetail() == "") {
            resultObject.setMess("添加失败");
        } else {
            resultObject.setMess("添加成功");
            resultObject.setResult(blogTextMapperService.addBlogText(blogText));
        }
        return resultObject;
    }

    /*获得数据库总数据*/
    @RequestMapping("count")
    public int countBlogText(BlogText blogText) {
        int i = blogTextMapperService.countBlogText(blogText);
        return i;
    }

    @RequestMapping("updateBlogTextById")
    public ResultObject updateBlogTextById(BlogText blogText) {
        resultObject.setResult(blogTextMapperService.editBlogText(blogText));
        return resultObject;
    }

    @RequestMapping("selectByBlogid")
    public ArrayList<BlogText> selectByBlogid(BlogText blogText) {
        ArrayList<BlogText> blogTexts = blogTextMapperService.selectByBlogid(blogText);
        return blogTexts;
    }

    @RequestMapping("updateClickNumber")
    public ResultObject updateClickNumber(BlogText blogText) {
        resultObject.setResult(blogTextMapperService.addClickNumber(blogText));
        return resultObject;
    }

    @RequestMapping("deleteBackById")
    public ResultObject deleteBackById(BlogText blogText) {
        int i = blogTextMapperService.removeBlogText(blogText);
        if (i == 1) {
            resultObject.setStatus(400);
            resultObject.setResult("删除成功");
        } else {
            resultObject.setStatus(404);
            resultObject.setResult("删除失败");
        }
        return resultObject;
    }
}
